package com.zp.resource.common;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

import com.sun.jersey.spi.resource.Singleton;
import com.zp.bean.common.OperationLogVo;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.common.ResultOperationLogVo;
import com.zp.domain.OperationLog;
import com.zp.domain.Type;
import com.zp.domain.User;
import com.zp.util.Constant;
import com.zp.util.MyException;

/**
 * operation log services
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/2/12
 * 
 */
@Component
@Path("/rest/operationLog")
@Singleton
public class OperationLogServices extends BaseServices {

	/**
	 * add one Log path:/operationLog/addLog Http:POST
	 * 
	 * @param operationLog
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 */
	@POST
	@Path("/addLog")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response addLog(OperationLog operationLog) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.save(operationLog);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * update one Log path:/operationLog/updateLog Http:PUT
	 * 
	 * @param operationLog
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 */
	@PUT
	@Path("/updateLog")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response updateLog(OperationLog operationLog) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.update(operationLog);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * delete Log by id path:/operationLog/deleteLog Http:DELETE
	 * 
	 * @param logId
	 *            the primary key of OperationLog
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 */
	@DELETE
	@Path("/deleteLog")
	@Produces(MediaType.APPLICATION_JSON)
	public Response deleteLog(@QueryParam("logId") String logId) {
		logger.info("deleteLog called logId:" + logId);
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.deleteById(OperationLog.class, logId);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * get Log by id path:/operationLog/getLog Http:GET
	 * 
	 * @param logId
	 *            the primary key of OperationLog
	 * 
	 * @return Response,ResultOperationLogVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":1}
	 */
	@GET
	@Path("/getLog")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getLogById(@QueryParam("logId") String logId) {
		logger.info("getLogById called logId:" + logId);
		ResultOperationLogVo result = new ResultOperationLogVo();
		try {
			OperationLog log = (OperationLog) jpaDao.findById(
					OperationLog.class, logId);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
			OperationLogVo vo = new OperationLogVo();
			vo.setLogDate(log.getLogTime());
			vo.setType(this.getTypeByCode(log.getTypeCode()));
			vo.setUser((User) this.jpaDao.findById(User.class, log.getUserId()));
			result.setLog(vo);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * 
	 * get operationLog with page parameters
	 * path:/operationLog/getLogs Http:GET
	 * 
	 * @param pageNo
	 * 
	 * @param pageSize
	 * 
	 * @return Response ResultOperationLogVo with JSON String
	 *         like:{"code":268,"message"
	 *         :"OK","totalCount":4,"log":null,"listVo"
	 *         :[{"user":{"id":"402881e64459ad0e014459adc3b00001"
	 *         ,"name":"Alex","birthday"
	 *         :null,"gender":null,"certNumber":null,"typeCode"
	 *         :null,"registerTime"
	 *         :1393073964000,"updateTime":null},"type":{"id"
	 *         :"402881e64458d2bb014458eb7fb6000f"
	 *         ,"name":"删除数据","code":"D00001",
	 *         "parentCode":"DA0001"},"logDate":1393079012000
	 *         },{"user":{"id":"402881e64459ad0e014459adc3b00001"
	 *         ,"name":"Alex","birthday"
	 *         :null,"gender":null,"certNumber":null,"typeCode"
	 *         :null,"registerTime"
	 *         :1393073964000,"updateTime":null},"type":{"id"
	 *         :"402881e64458d2bb014458eb1665000e"
	 *         ,"name":"查询数据","code":"S00001",
	 *         "parentCode":"DA0001"},"logDate":1393078998000}]}
	 */
	@GET
	@Path("/getLogs")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response getLogWithPages(@QueryParam("pageNo") int pageNo,
			@QueryParam("pageSize") int pageSize) {
		logger.info("getLogWithPages called json param: pageNo is:" + pageNo
				+ " pageSize is:" + pageSize);
		ResultOperationLogVo result = new ResultOperationLogVo();
		List<OperationLogVo> listVo = new ArrayList<OperationLogVo>();
		try {
			String jpql = "from OperationLog order by logTime desc";
			List<?> list = this.jpaDao.getResultData(pageNo, pageSize, jpql,
					null);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
			if (list != null && list.size() > Constant.ZERO) {
				for (Object o : list) {
					OperationLog log = (OperationLog) o;
					OperationLogVo vo = new OperationLogVo();
					vo.setLogDate(log.getLogTime());
					vo.setType((Type) this.getTypeByCode(log.getTypeCode()));
					vo.setUser((User) this.jpaDao.findById(User.class,
							log.getUserId()));
					listVo.add(vo);
				}
				result.setListVo(listVo);
				result.setTotalCount(this.jpaDao.getCount(
						"select count(id) from OperationLog", null));

			}
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}

		return Response.ok(result).build();
	}

}
